package com.common.common.msg;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 返回结果
 * @param <T> 返回数据类型
 *
 * @author liujun
 * @date 2024/10/10
 */
@Data
@NoArgsConstructor
public class ResultMsg<T> {
    private int code;
    private String msg;
    private T data;
    public ResultMsg(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResultMsg(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <D> ResultMsg<D> success(String msg) {
        return new ResultMsg<>(200, msg);
    }

    public static <D> ResultMsg<D> success(String msg, D data) {
        return new ResultMsg<>(200, msg, data);
    }

    public static <D> ResultMsg<D> error(String msg) {
        return new ResultMsg<>(500, msg);
    }

    public static <D> ResultMsg<D> error(String msg, D data) {
        return new ResultMsg<>(500, msg, data);
    }

    public static <D> ResultMsg<D> warn(String msg) {
        return new ResultMsg<>(400, msg);
    }

    public static <D> ResultMsg<D> warn(String msg, D data) {
        return new ResultMsg<>(400, msg, data);
    }

    public boolean isSuccess() {
        return code == 200;
    }

    public boolean isWarn() {
        return code == 400;
    }

    public boolean isError() {
        return code == 500;
    }
}
